package test;

import org.dreamwork.jasmine2.engine.JasmineRequest;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.events.IClickListener;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.Button;
import org.dreamwork.jasmine2.web.controls.FileControl;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.util.VirtualFile;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 14-9-3
 * Time: 下午2:22
 */
public class UploadPage extends Page {
    protected FileControl file;
    protected Button btnUpload;

    @Override
    public void onPagePreload (Page page) throws EventException {
        super.onPagePreload (page);
        btnUpload.addClickListener (new IClickListener () {
            @Override
            public void onClick (IWebControl sender) throws EventException {
                try {
                    save ();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        });
        file.setViewStateEnabled (false);
    }

    @Override
    public void onPageLoad (Page page) throws EventException {
        super.onPageLoad (page);
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        super.onPageLoadCompleted (page);
    }

    private void save () throws IOException {
/*
        VirtualFile vf = file.getVirtualFile ();
        try {
            if (vf != null) vf.save ("d:/tmp/");
        } catch (IOException e) {
            e.printStackTrace ();
        }
*/
        VirtualFile[] files = ((JasmineRequest) request).getVirtualFiles ();
        for (VirtualFile f : files) {
            f.save ("d:/tmp/");
        }
    }
}
